home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte0887.arc / SZPAK.LST < prev    next >
File List  |  1987-07-14  |  3KB  |  140 lines

  1. --------------------------------------------------------------
  2. SZPAK.LST  The following 5 listings accompany the article
  3. "Logic Grammars", by Stan Szpakowicz, in the August 1987 issue
  4. of BYTE, page 185.
  5. --------------------------------------------------------------
  6. Listing 1:
  7. «MDBO»
  8. statements --> statement, [';'], statements.
  9. statements --> [].
  10. statement --> [skip].
  11. statement --> [id(V)], [:=], expr.
  12. statement --> [if], condition, [then], statements, [fi].
  13. statement --> [while], condition, [do], statements, [od].
  14.  
  15. condition --> [not], relation.
  16. condition --> relation.
  17. relation --> expr, comp_op, expr.
  18. comp_op --> ['='].
  19. comp_op --> ['<'].
  20.  
  21. expression --> primary.
  22. expression --> expression, arith_op, primary.
  23.  
  24. primary --> [id(V)].
  25. primary --> [num(N)].
  26.  
  27. arith_op --> ['+'].
  28. arith_op --> ['-'].
  29. arith_op --> ['*'].
  30. arith_op --> ['/'].
  31. «MDNM»
  32. [end listing 1]
  33.  
  34. Listing 2:
  35. «MDBO»
  36.  /*1*/  statement(K, N)  :-
  37.          token(id(V), K, L), token(:=, L, M), expr(M, N).
  38.  /*2*/  expr(K, L) :- primary(K, L).
  39.  /*3*/  expr(K, N) :- expr(K, L), arith_op(L, M), primary(M, N).
  40.  
  41.  /*4*/  primary(K, L) :- token(id(V), K, L).
  42.  /*5*/  primary(K, L) :- token(num(V), K, L).
  43.  
  44.  /*6*/  arith_op(K, L) :- token(+, K, L).
  45.  
  46.  /*7*/  token(T, [T|Ts], Ts).
  47. «MDNM»
  48.  
  49. [end listing 2]
  50.  
  51. «PG»
  52. Listing 3:
  53. «MDBO»
  54. program(s(Stmt, Stmts)) -->
  55.         statement(Stmt), [';'],
  56.         statements(Stmts).
  57.  
  58. statements(s(Stmt, Stmts))  -->
  59.         statement(Stmt), [';'],
  60.         statements(Stmts).
  61. statements(skip) --> [].
  62.  
  63. % a sequence of statements is represented as a nested term,
  64. % for example s(Stmt1, s(Stmt2, s(Stmt3, skip))),
  65. % where Stmt1, Stmt2, Stmt3 represent individual statements 
  66.  
  67. statement(skip) --> [skip].
  68. statement(let(V, E)) --> [id(V)], [:=], expr(E).
  69. statement(if(C, Stmts))  -->
  70.         [if], condition(C), [then], statements(Stmts), [fi].
  71. statement(while(C, Stmts))  -->
  72.         [while], condition(C), [do], statements(Stmts), [od].
  73.  
  74. condition(not(C)) --> [not], relation(C).
  75. condition(C) --> relation(C).
  76. relation(cond(Op, E1, E2)) --> expr(E1), comp_op(Op), expr(E2).
  77.  
  78. comp_op('=') --> ['='].
  79. comp_op('<') --> ['<'].
  80. «MDNM»
  81. [end listing 3]
  82.  
  83. Listing 4:
  84.  
  85. «MDBO»
  86. interm_code(s(Stmt, Stmts))  -->
  87.         interm_code(Stmt), interm_code(Stmts).
  88. interm_code(skip) --> [].
  89. interm_code(let(V, E))  -->
  90.         expr_interm_code(E), [store(V)].
  91. interm_code(if(C, Stmts))  -->
  92.         { newlabel(L) },
  93.         cond_interm_code(not(C)),
  94.         [jmp_cond(L)],
  95.         interm_code(Stmts),
  96.         [label(L)].
  97. interm_code(while(C, Stmts))  -->
  98.         { newlabel(L1) }, { newlabel(L2) },
  99.         [label(L1)],
  100.         cond_interm_code(not(C)),
  101.         [jmp_cond(L2)],
  102.         interm_code(Stmts),
  103.         [jmp(L1)], [label(L2)].
  104. «MDNM»
  105.  
  106. [end listing 4]
  107.  
  108. Listing 5:
  109.  
  110. The source program:
  111. «MDBO»
  112. x := a;  y := n;  z := 1;
  113. while not i < 1 do
  114.   if y / 2 * 2 < y then
  115.     z := z * x;
  116.   fi;
  117.   x := x * x;
  118.   y := y / 2;
  119. od; #
  120. «MDNM»
  121. The resulting object code:
  122. «MDBO»
  123. load(a)         
  124. store(x)        
  125. load(n)         
  126. store(y)        
  127. loadc(1)        
  128. store(z)        
  129. label($lbl1)    
  130. loadc(1)        
  131. store($mem1)    
  132. load(i)         
  133. sub($mem1)      
  134. tst_neg         
  135. jmp_cond($lbl2) 
  136.  
  137. etc.
  138. «MDNM»
  139. [end listing 5]
  140.